/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.drmundo.parsing;

import com.drmundo.entity.Summoner;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Raphael
 */
// classe pour parser le JSON
@WebServlet(name = "ParseJSON", urlPatterns = {"/test"})
public class ParseJson extends HttpServlet {

    private String json;
 

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            json = readUrl();
        } catch (Exception ex) {
            Logger.getLogger(ParseJson.class.getName()).log(Level.SEVERE, null, ex);
        }


        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(json);


        JsonObject saleObject = element.getAsJsonObject();



        JsonObject summonerObject = saleObject.getAsJsonObject("data");

        Summoner summoner = new Summoner();

        summoner.setInternalName(summonerObject.get("internalName").getAsString());
        summoner.setName(summonerObject.get("name").getAsString());
        summoner.setAcctId(summonerObject.get("acctId").getAsLong());
        summoner.setProfileIconId(summonerObject.get("profileIconId").getAsInt());
        summoner.setRevisionDate(summonerObject.get("revisionDate").getAsString());
        summoner.setSummonerLevel(summonerObject.get("summonerLevel").getAsInt());
        summoner.setSummonerId(summonerObject.get("summonerId").getAsInt());
       // summoner.setSuccess(summonerObject.get("success").getAsBoolean());

        System.out.println("summoner " + summoner);

        
            req.setAttribute("summoner", summoner);
            req.getRequestDispatcher("/index_1.jsp").forward(req, resp);
            
 

    }



    private static String readUrl() throws Exception {

        BufferedReader reader = null;
        try {
            URL url = new URL("http://api.elophant.com/v2/euw/summoner/chacha974?key=6LfOQJzS60e8JqBu9cg2");
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuffer buffer = new StringBuffer();

            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1) {
                buffer.append(chars, 0, read);
            }

            return buffer.toString();
        } finally {
            if (reader != null) {
                reader.close();
            }

        }


    }
}